package zzz.jsondemo;

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Field;

/**
 * Created by 隼 on 2017/5/17.
 */
public class Gsondemo {
    public static void main(String[] args) {
        Diaosi test=new Diaosi();
        test.setName("王小二");
        test.setAge(25.2);
        test.setBirthday("1990-01-01");
        test.setHas_girlfriend(false);
        test.setCar(null);
        test.setMajor(new String[] {"理发","挖掘机"});
        test.setHouse(null);
        test.setIgnore("IGOngg");
        GsonBuilder gsonBuilder=new GsonBuilder();
        //格式化输出json
        gsonBuilder.setPrettyPrinting();
            //生成过程中 做一些操作
        gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
            public String translateName(Field field) {
                if(field.getName().equals("name"))
                    return "N33AME";
                return field.getName();
            }
        });
//        Gson gson=new Gson();
        Gson gson=gsonBuilder.create();
        System.out.println(gson.toJson(test).toString());
    }
}
